#encoding=utf-8
'''
Created on 2011-11-17

@author: guanyw

python version:2.6
'''
from ltp.ltp_interface import NER, CountWordInDocument,\
    GetWord, GetNE, GetPOS, CreateDOMFromString, IRLAS
import re
from corpus.preprocess import translate_number, year_translate, rep_num

def get_words_and_nes(sents):
    CreateDOMFromString(sents.encode('gbk'))
    NER()
#    IRLAS()
    wds_num = CountWordInDocument()
    wd_list = [GetWord(i).decode('gbk') for i in range(wds_num)]
    ne_list = [(GetWord(i).decode('gbk'),GetNE(i)) for i in range(wds_num) if GetNE(i)!='O']
    return (wd_list,ne_list)

def get_tagged_sentence(sents):
    '''
            给句子加标注。返回句子的格式如下：
            例如：'诺贝尔奖得主重回'
        '诺贝尔奖 /pos_tag /ne_tag 得主 /pos_tag /ne_tag 重回 /pos_tag /ne_tag'
            如果原句中含有空格先去除所有的空格
    '''
    re.sub('\s+',"",sents)
    sents = rep_num(sents)
#    sents,flg = year_translate(sents)
    
    CreateDOMFromString(sents.encode('gbk','ignore'))
#   IRLAS()
    NER()
    wds_num=CountWordInDocument()
    wd_list = [GetWord(i).decode('gbk') for i in range(wds_num)]
    pos_list=[GetPOS(i) for i in range(wds_num)]
    ne_list = [GetNE(i) for i in range(wds_num)]
    
#    wd_joined = ''.join([translate_number(wd_list[i],pos_list[i]) for i in range(wds_num)])
#    if wd_joined != sents:
#        CreateDOMFromString(wd_joined.encode('gbk','ignore'))
#        NER()
#        wds_num=CountWordInDocument()
#        wd_list = [GetWord(i).decode('gbk') for i in range(wds_num)]
#        pos_list=[GetPOS(i) for i in range(wds_num)]
#        ne_list = [GetNE(i) for i in range(wds_num)]
        
    temp=""
    for i in range(wds_num):
        temp+=wd_list[i]+' /'+pos_list[i]+' /'+ne_list[i]+' '
    
#    print temp
    return temp

def print_list (li):
    for ls in li:
        print ls
    
if __name__ == '__main__':
    pass 
    s = get_tagged_sentence('阿尔马兹别克·阿坦巴耶夫赢得总统选举')
    print s
#    wd_list,ne_list = get_words_and_nes('诺贝尔奖得主重回中国')
#
#    print_list (wd_list)
#    print_list (ne_list)